﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Management;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Util.Controls;


namespace FalionForm
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        public static string Culture { get; set; }
        protected override void OnStartup(StartupEventArgs e)
        {
            Thread.Sleep(3000);
            this.Exit += App_Exit;
            GetCulture();
           
            Startup(e);
        }

        private void App_Exit(object sender, ExitEventArgs e)
        {
            SetCulture();
            PublicData.save("","abc.ini");
        }

        #region Method
        private void GetCulture()
        {
            Culture = string.Empty;
            try
            {
                Culture = FalionForm.Properties.Settings.Default.Culture.Trim();
            }
            catch (Exception)
            {
            }
            Culture = string.IsNullOrEmpty(Culture) ? "en-US" : Culture;

            //update culture
            UpdateCulture();
        }
        public static void SetCulture()
        {
            try
            {
                FalionForm.Properties.Settings.Default.Culture = Culture;
                FalionForm.Properties.Settings.Default.Save();
            }
            catch (Exception)
            {
            }
        }
        public static void UpdateCulture()
        {
            List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
            foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
            {
                dictionaryList.Add(dictionary);
            }
            string requestedCulture = string.Format(@"Language\StringResource.{0}.xaml", Culture);
            ResourceDictionary resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture));
            if (resourceDictionary == null)
            {
                requestedCulture = @"Language\StringResource.en-US.xaml";
                resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture));
            }
            if (resourceDictionary != null)
            {
                Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Culture);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(Culture);

            //MessageBoxManager.Unregister();
            //MessageBoxManager.OK = Application.Current.TryFindResource("BtnOK").ToString();
            //MessageBoxManager.Register();
        }
        #endregion



        private static System.Threading.Mutex mutex;
        private new void Startup(StartupEventArgs e)
        {
            try
            {
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                string str = "";
                foreach (ManagementObject mo in moc)
                {
                    str = mo.Properties["ProcessorId"].Value.ToString();
                    break;
                }
                if (str == "BFEBFBFF000806EA" || str == "BFEBFBFF000906E9" || str == "BFEBFBFF000906E9"|| str == "BFEBFBFF000406E3")
                {
                   
                }
                else
                {
                    return;
                }
            }
            catch
            {
            }
            try
            {
                PublicData.read("", "abc.ini");
            }
            catch 
            {
                PublicData.read("", "Temp/abc.ini");           
            }
            
            mutex = new System.Threading.Mutex(true, "OnlyRun_CR");
            if (mutex.WaitOne(0, false))
            {               
                base.OnStartup(e);
            }
            else
            {
                MessageBoxX.Info("软件已打开，点击确定关闭");
                this.Shutdown();
            }

        }

    }
}
